package com.ztf0337.mycurriculumdesign.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.CompareClassUtil;
import com.ztf0337.mycurriculumdesign.entity.Order;
import com.ztf0337.mycurriculumdesign.entity.User;

import java.util.List;

public class UserAdapter  extends RecyclerView.Adapter<UserAdapter.ViewHolder>{

    private List<User> userList;
    private UserAdapter.OnItemClickListener listener;  // 监听器引用
    public UserAdapter(List<User> userList) {
        this.userList = userList;
    }

    // 定义点击事件接口

    public interface OnItemClickListener {
        void onItemClick(User user);
    }
    // 提供设置监听器的方法
    public void setOnItemClickListener(UserAdapter.OnItemClickListener listener) {
        this.listener = listener;
    }


    @NonNull
    @Override
    public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.m_item_user, parent, false);
        return new UserAdapter.ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, int position) {
        User user = userList.get(position);
        holder.tv_user_id.setText("ID："+user.getId());
        if (user.getStatus()){
            holder.tv_user_status.setTextColor(Color.GREEN);
        } else {
            holder.tv_user_status.setTextColor(Color.RED);
        }

        holder.tv_user_status.setText("状态："+(user.getStatus() ? "正常" : "封禁"));
        holder.tv_user_phone.setText("电话："+user.getPhone());
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(user);
            }
        });

    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_user_id, tv_user_status,tv_user_phone;
        public ViewHolder(View view) {
            super(view);
            tv_user_id = view.findViewById(R.id.tv_user_id);
            tv_user_status = view.findViewById(R.id.tv_user_status);
            tv_user_phone = view.findViewById(R.id.tv_user_phone);
        }
    }

    // 更新整个数据集
    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<User> newData) {
        this.userList = newData;
        notifyDataSetChanged();  // 简单刷新全部（低效）
    }

    // 修改一项
    public void updateItem(User newItem) {
        userList.set(CompareClassUtil.compareUser(userList,newItem), newItem);
        notifyItemChanged(CompareClassUtil.compareUser(userList,newItem));
    }

    // 删除一项
    public void removeItem(User newItem) {
        int index = CompareClassUtil.compareUser(userList, newItem);
        if (index != -1) {
            userList.remove(index);
            notifyItemRemoved(index);
        }
    }

}
